package io.renren.modules.back.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.back.entity.SpecificationEntity;
import io.renren.modules.back.service.SpecificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 规格接口
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年1月26日 上午10:40:10
 */
@RestController
@RequestMapping("/back/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    /**
     * 查看列表
     */
    @GetMapping("/list")
    @RequiresPermissions("specification:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);
        return R.ok().put("data", page);
    }

    /**
     * 查看信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("specification:info")
    public R info(@PathVariable("id") Long id){
        SpecificationEntity specificationEntity = specificationService.getById(id);
        return R.ok().put("data", specificationEntity);
    }

    /**
     * 保存
     */
    @SysLog("保存规格")
    @PostMapping("/save")
    @RequiresPermissions("specification:save")
    public R save(@RequestBody SpecificationEntity specificationEntity){
        ValidatorUtils.validateEntity(specificationEntity);
        specificationService.save(specificationEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改规格")
    @PostMapping("/update")
    @RequiresPermissions("specification:update")
    public R update(@RequestBody SpecificationEntity specificationEntity){
        ValidatorUtils.validateEntity(specificationEntity);
        specificationService.updateById(specificationEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除规格")
    @PostMapping("/delete")
    @RequiresPermissions("specification:delete")
    public R delete(@RequestBody Long[] ids){
        specificationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
